﻿using Customer.API.Applcation.Command.FinanceCommand;
using Customer.Domain;
using Customer.Infrstructre;
using Customer.Infrstructre.FinanceManager;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Customer.API.Applcation.CommandHandlers.FinanceCommandHandler
{
    /// <summary>
    /// 新增实收中介者
    /// </summary>
    public class ADDPaymentCommandHandler:IRequestHandler<ADDPaymentCommand,int>
    {
        private readonly IPaymentRepository _payRep;
            private readonly IReceivableRepository _ReceRep;
        private readonly EFCoreDBcontext DB;

        public ADDPaymentCommandHandler(IPaymentRepository payRep, IReceivableRepository receRep, EFCoreDBcontext dB)
        {
            _payRep = payRep;
            _ReceRep = receRep;
            DB = dB;
        }

        public async Task<int> Handle(ADDPaymentCommand request, CancellationToken cancellationToken)
        {
            using (var tran = DB.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
            {
                try
                {
                    //实收信息表添加数据
                    Payment payment = new Payment();
                    payment.PaymentId = request.PaymentId;
                    payment.SerialNumber = request.SerialNumber;
                    payment.PaymentName = request.PaymentName;
                    payment.FinanceTypeId = request.FinanceTypeId;
                    payment.PaymentTime = request.PaymentTime;
                    payment.PaymentPrice = request.PaymentPrice;
                    payment.ResidueMoney = request.ResidueMoney;
                    payment.PaymentRemark = request.PaymentRemark;
                    payment.Office_Id = request.Office_Id;
                    payment.User_Id = request.User_Id;
                    await _payRep.AddAsync(payment);
                    //修改应收表数据
                    var res = await _ReceRep.GetAllAsync();
                    var plist = res.Where(x => x.SerialNumber == payment.SerialNumber).FirstOrDefault();
                    if (plist != null)
                    {
                        plist.ReceivableId = plist.ReceivableId;
                        plist.PaymentPrice += payment.PaymentPrice;
                        plist.ResidueMoney -= payment.PaymentPrice;
                        if (plist.ResidueMoney != plist.ReceivableAmount)
                        {
                            plist.ReceivableState = 4;
                        }
                        if (plist.ResidueMoney ==0)
                        {
                            plist.ReceivableState = 5;
                        }
                        await _ReceRep.Update(plist);
                    }
                    var code = await DB.SaveChangesAsync();
                    tran.Commit();
                    return code;
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }


            }
        }
    }
}
